package com.zsh.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zsh
 * @Description
 * @create 2022-02-19 16:23
 */

@Controller
public class RequestController {

    @GetMapping("/goto")
    public String gotoPage(Model model){
        model.addAttribute("msg","成功");
        model.addAttribute("code",200);
        return "forward:success"; //请求转发
    }

    @GetMapping("/params")
    public String testParam(Map<String, Object> map,
                            Model model,
                            HttpServletRequest request,
                            HttpServletResponse response){
        map.put("hello","world666");
        model.addAttribute("world","hello666");
        request.setAttribute("message","helloWorld");
        Cookie cookie = new Cookie("c1","v1");
        cookie.setDomain("localhost");
        response.addCookie(cookie);
        return  "forward:success";
    }

    @ResponseBody
    @GetMapping("/success")
    public Map<String, Object> success(//@RequestAttribute("msg") String msg,
                          //@RequestAttribute("code") Integer code,
                                       //@ModelAttribute("msg") String msg1,
                                       HttpServletRequest request){ //model.getAttribute 问题
//        Object msg1 = model.getAttribute("msg");
//        Object code1 = model.getAttribute("code");
        Map<String, Object> map = new HashMap<>();
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");
        //map.put("msg1",msg1);
//        map.put("code1",code1);
        //map.put("msg",msg);
        //map.put("code",code);
        map.put("hello",hello);
        map.put("world",world);
        map.put("message",message);
        return map;
    }
}
